package com.qq2008.game.stzb.interceptor;

import com.qq2008.game.stzb.jdo.Role;
import com.qq2008.game.stzb.server.RoleServer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

// 魔界角色登陆拦截器
public class MoAuthInterceptor extends HandlerInterceptorAdapter {

    // 不用拦截的路径
    private final String excludeCMDList[] = {"server", "main", "createRole", "login"};

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String cmd = request.getParameter("cmd");
        HttpSession session = request.getSession();
        if (!Arrays.asList(excludeCMDList).contains(cmd)) {
            Object object = session.getAttribute("roleId");
//            System.out.println(object);
            if (null == object) {
                response.sendRedirect("/mojie/game?cmd=main");
                return false;
            }
            int roleId = (int) object;
            Role role = RoleServer.getRole(roleId);
//            System.out.println(JSON.toJSONString(role));
            if (null == role) {
                response.sendRedirect("/mojie/game?cmd=main");
                return false;
            }
        }
        return true;
    }

}
